<?php

namespace App\Admin\Controllers\Order;


use App\Admin\Extensions\Column\OpenNote;
use App\Admin\Extensions\Tools\OrderStatus;
use App\Models\Goods\Goods;
use App\Models\Sys\Exchange;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Widgets\Box;
use Encore\Admin\Widgets\Table;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ExchangeController extends Controller
{
    use ModelForm;
    protected $header = '订单列表';
    /**
     * Index interface.
     *
     * @return Content
     */
    public function index(Request $request)
    {
        return Admin::content(function (Content $content) use ($request) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->grid($request));
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid(Request $request)
    {
        return Admin::grid(Exchange::class, function (Grid $grid) use ($request) {

            /*$status = $request->get('status', 0);

            $grid->model()->when($status,function ($query) use ($status) {
                    return $query->where('pay_status',$status);
                })->orderBy('id','desc');*/

            $grid->model()->orderBy('id','desc');
            $grid->id('id');
            $grid->order_sn('订单编号');
            $grid->create_at('提交时间');
            $grid->user()->username('用户账号');
            $grid->user()->telephone('手机号');
            $grid->money('消费校币');
            $grid->is_delivery('是否发货')->display(function ($v){
                return $v == '1' ? '是' : ($v == '2' ? '否' : '-');
            });
            $grid->status('订单状态')->display(function ($v){
                return $v == '1' ? '兑换' : ($v == '9' ? '取消' : '-');
            });

            //去除创建按钮
            $grid->disableCreateButton();

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableEdit();
                $actions->disableView();
                $actions->disableDelete();

                $actions->append('<a href="'.$actions->getResource().'/'.$actions->getKey().'"><i class="fa fa-eye"></i>查看订单</a>');

                if ($actions->row->is_delivery == '1') {
                    //$actions->addDeleteOrder();
                } else if ($actions->row->is_delivery == '2') {
                    $actions->append('<a href="/deliver?id='.$actions->getKey().'"><i class="fa fa-paper-plane"></i>订单发货</a>');
                } else {
                    //$actions->addCloseOrder();
                }


            });

            //搜索栏
            $grid->filter(function (Grid\Filter $filter) {
                $filter->like('order_sn','订单编号');
            });



            /*$grid->order_sn('订单编号');
            $grid->create_at('提交时间')->display(function ($v){
                return date('Y-m-d H:i:s', $v);
            });
            $grid->user()->tp_name('用户账号');
            $grid->mobile('手机号');
            $grid->order_amount('消费校币');
            $grid->pay_type('支付方式')->display(function ($v){
                if ($this->pay_status == '1'){
                    return '未支付';
                } else if ($this->pay_status == '2'){
                    return $this->pay_type == '3' ? '支付宝' : ($this->pay_type == '4' ? '微信' : '其他');
                } else {
                    return '其他';
                }
            });
            $grid->mobile_type('订单来源')->display(function ($v){
                return $v == '1' ? 'android' : ($v == '2' ? 'ios' : '-');
            });
            $grid->pay_status('订单状态')->display(function ($v){
                return $v == '1' ? '待付款' : ($v == '2' ? '待发货' : '-');
            });

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableEdit();
                $actions->disableView();
                $actions->disableDelete();

                $actions->append('<a href="'.$actions->getResource().'/'.$actions->getKey().'"><i class="fa fa-eye"></i>查看订单</a>');

                if ($actions->row->pay_status == '1') {
                    $actions->addCloseOrder();
                } else if ($actions->row->pay_status == '2') {
                    $actions->append('<a href=""><i class="fa fa-paper-plane"></i>订单发货</a>');
                } else {
                    $actions->addDeleteOrder();
                    //$actions->append('<a href="javascript:void(0);" data-id="'.$actions->getKey().'" class="grid-row-delete"><i class="fa fa-trash"></i>删除订单</a>');
                }


            });

            //去除创建按钮
            $grid->disableCreateButton();

            //搜索栏
            $grid->filter(function (Grid\Filter $filter) {
                $filter->disableIdFilter();
                $filter->column(1/3,function ($filter){
                    $filter->like('order_sn','订单编号');
                });
                $filter->column(1/3,function ($filter){
                    $filter->like('mobile','手机号');
                });
                $filter->column(1/3,function ($filter){
                    $filter->between('create_time', '创建时间')->datetime();
                });
            });*/


            /*$row = Info::select(['pay_status',DB::raw('COUNT(*) num')])
                ->groupBy('pay_status')->get()->toArray();

            $data = [];
            foreach ($row as $k=>$v){
                $data[$v['pay_status']] = $v['num'];
            }

            $options = [
                '0' => '全部订单',
                '1' => '待付款',
                '2' => '待发货',
                '3' => '已关闭',
                '4' => '已发货',
                '5' => '已完成',
            ];

            foreach ($options as $key=>$val){
                if ($key == 0 ){
                    $options[$key] = $val . '('.array_sum($data).')';
                } else {
                    if (isset($data[$key])){
                        $options[$key] = $val . '('.$data[$key].')';
                    } else {
                        $options[$key] = $val . '(0)';
                    }
                }
            }

            $grid->tools(function ($tools) use ($options) {
                $tools->append(new OrderStatus($options));
            });*/


        });
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return Content
     */
    public function show($id, Content $content)
    {
        $content->header('订单详情');

        $data = Exchange::with('address')
            ->with('user')
            ->with('goods')
            ->with('express')
            ->where('id',$id)
            ->orderBy('id','asc')
            ->first()->toArray();

        $content->row(view('admin.back'));

        if ($data){

            $express = '未发货';
            if (isset($data['express']['express_sn']) && $data['express']['express_sn']){
                $express = $data['express']['express_sn'];
            }

            $headers = ['订单编号', '物流单号', '用户账号', '支付方式', '支付校币'];
            $rows = [
                [$data['order_sn'], $express , @$data['user']['username'], '', $data['money']],
            ];
            $table = new Table($headers, $rows);
            $content->row((new Box('基本信息', $table))->solid()->style('primary'));

            $headers = ['收货人', '手机号码', '收货地址'];
            $rows = [
                [@$data['address']['linkman'], @$data['address']['mobile'] , @$data['address']['address']],
            ];
            $table = new Table($headers, $rows);
            $content->row((new Box('收货人信息', $table))->solid()->style('primary'));

            $content->row(view('admin.order.goods',compact('data')));

        } else {
            abort(404);
        }

        return $content;


        /*return $content
            ->header($this->header)
            ->description('Detail')
            ->row($this->detail($id));*/
    }


    
}
